package org.zizi.util;

import java.security.MessageDigest;
import java.util.Arrays;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class PasswordChecher
{
	public static boolean check(String s, String s1, String s2) throws Exception
	{
		BASE64Encoder encoder = new BASE64Encoder();
		BASE64Decoder decoder = new BASE64Decoder();
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		
		byte[] abyte0 = decoder.decodeBuffer(s2);
		md5.update(abyte0);
		md5.update(s.getBytes("UTF-8"));
		
		byte[] abyte1 = md5.digest();
		String s3 = encoder.encode(abyte1);
		
		byte[] abyte2 = s1.getBytes();
		byte[] abyte3 = s3.getBytes();
		
		return Arrays.equals(abyte2, abyte3);
	}
	
	public static boolean check(String plaintext , String ciphertext) throws Exception
	{
		String p1 = ciphertext.substring(16);
		String p2 = ciphertext.substring(0, 16);
		
		return check(plaintext, p1, p2);
	}
}
